package kxfang.com.common;


import android.app.Application;
import android.content.Context;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;
import kxfang.com.base.base.BaseApplication;
import kxfang.com.common.constant.Constant;

import com.hjq.umeng.UmengClient;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mmkv.MMKV;

/**
 * 应用模块:
 * <p>
 * 类描述: 通用库 & 基础库 自身初始化操作
 * <p>
 *
 * @author darryrzhoong
 * @since 2020-02-25
 */
public class CommonModuleInit implements IModuleInit
{
    @Override
    public boolean onInitAhead(BaseApplication application)
    {
        // 初始化日志
        Logger.addLogAdapter(new AndroidLogAdapter()
        {
            @Override
            public boolean isLoggable(int priority, @Nullable String tag)
            {
                return application.issDebug();
            }
        });
        if (application.issDebug())
        {
            ARouter.openLog(); // 开启日志
            ARouter.openDebug(); // 使用InstantRun的时候，需要打开该开关，上线之后关闭，否则有安全风险
        }
        Hawk.init(application).build();
        Constant.init("Debug");
        ARouter.init(application);
        MMKV.initialize(application);
        initBugly(application);
        UmengClient.init(application);
        Logger.i("基础层初始化完毕 -- onInitAhead");


        return false;
    }

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.common_color_66, R.color.common_colorWhite);//全局设置主题颜色
            return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            return new ClassicsFooter(context).setDrawableSize(20).setSpinnerStyle(SpinnerStyle.Translate);
        });
    }
    
    @Override
    public boolean onInitLow(BaseApplication application)
    {
        return false;
    }

    /**
     * 初始化bugly
     */
    private void initBugly(Application application) {
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(application);
        strategy.setUploadProcess(false);

        CrashReport.initCrashReport(application, "2d3721efb9", false, strategy);
    }
    
}
